package com.lzy.srb.core.controller.admin;

import com.lzy.common.exception.Assert;
import com.lzy.common.exception.BusinessException;
import com.lzy.common.result.R;
import com.lzy.common.result.ResponseEnum;
import com.lzy.srb.core.pojo.entity.IntegralGrade;
import com.lzy.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "积分等级")
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntergralGradeController {

    @Autowired
    IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll(){
        return R.ok().data("list",integralGradeService.list()).message("获取列表成功");
    }

    @ApiOperation(value = "根据id删除数据记录",notes = "逻辑删除数据")
    @DeleteMapping ("/remove/{id}")
    public R removeById(@PathVariable("id") Long id){

        boolean result = integralGradeService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(@RequestBody IntegralGrade integralGrade){

//        if(integralGrade.getBorrowAmount() == null) {
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(integralGrade);
        if(result){
            return R.ok().message("保存成功");
        }else{
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@PathVariable("id") Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if(integralGrade!=null){
            return R.ok().data("record",integralGrade);
        }else{
            return R.error().message("数据获取失败");
        }
    }

    @ApiOperation("修改积分等级")
    @PutMapping("/update")
    public R updateById(@RequestBody IntegralGrade integralGrade){
        boolean result = integralGradeService.updateById(integralGrade);
        if(result){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }



}
